import { v4 as uuid } from 'uuid';
import insert from '@feidao/web/atom/nodejs/db/insert';
import { IBaseTable } from '@feidao/web/atom/nodejs/db/interfaces';
import log from '@feidao/web/atom/nodejs/logger/log';

interface Message {
	systemid1: string;
	systemid2: string;
	// todo
	[field: string]: any;
}

interface Star extends IBaseTable {
	name: string;
	sex: 0 | 1;
}

interface FruitPrices extends IBaseTable {
	name: string;
	price: number;
}

interface Hobby extends IBaseTable {
	star: string;
	hobby: string;
}
interface Works extends IBaseTable {
	star: string;
	works: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const { systemid1, systemid2 } = msg;
	const { exec, prepare } = insert(action_id, session_id, spaceid, systemid1);
	prepare<FruitPrices>('fruitprices', [{ _id: uuid(), name: '番茄', price: 0.5 }]);
	const star_id = uuid();
	prepare<Star>('star', [{ _id: star_id, name: '悟能', sex: 1 }]);
	prepare<Hobby>('hobby', [{ _id: uuid(), star: star_id, hobby: '吃和睡' }]);
	const ret = await exec();
	if (ret.code !== 1) {
		return 'fail';
	}
	const { exec: exec2, prepare: prepare2 } = insert(action_id, session_id, spaceid, systemid2);
	prepare2<Works>('works', [{ _id: uuid(), star: star_id, works: '九吃钉耙' }, { _id: uuid(), star: star_id, works: '嫦娥仙子' }]);
	const ret2 = await exec2();
	log('finish');
	if (ret2.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
